package com.situ.firmadmin.controller;


import com.situ.firmadmin.annotation.MyLog;
import com.situ.firmadmin.pojo.Auth;
import com.situ.firmadmin.pojo.Notice;
import com.situ.firmadmin.pojo.User;
import com.situ.firmadmin.pojo.query.AuthQuery;
import com.situ.firmadmin.pojo.query.NoticeQuery;
import com.situ.firmadmin.pojo.vo.TypeCountVO;
import com.situ.firmadmin.service.INoticeService;
import com.situ.firmadmin.service.IUserService;
import com.situ.firmadmin.util.PageResult;
import com.situ.firmadmin.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sun
 * @since 2024-09-27
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
	
	@Autowired
	private INoticeService noticeService;
	@Autowired
	private IUserService userService;
	
	@GetMapping("/list")
	public Result selectByPage(NoticeQuery noticeQuery){
		User user=userService.getUser();
		PageResult pageResult=noticeService.selectByPage(noticeQuery,user);
		Map<String,Object> map=new HashMap<>();
		map.put("total",pageResult.getCount());
		map.put("list",pageResult.getData());
		return Result.ok(map);
	}
	
	@GetMapping("/inform")
	public Result selectByUserName(){
		User user=userService.getUser();
		List<Notice> list=noticeService.selectByUserName(user);
		return Result.ok(list);
	}
	
	@GetMapping("/informTotal")
	public Result informTotal(){
		User user=userService.getUser();
		Integer total=noticeService.informTotal(user);
		return Result.ok(total);
	}
	
	@MyLog(module = "添加公告")
	@PostMapping("/add")
	public Result insert(@RequestBody Notice notice){
		User user=userService.getUser();
		notice.setAdminName(user.getName());
		noticeService.insert(notice);
		return Result.ok("添加成功！");
	}
	
	@GetMapping("/selectById/{id}")
	public Result selectById(@PathVariable Integer id){
		Notice notice=noticeService.selectById(id);
		System.out.println(notice);
		return Result.ok(notice);
	}
	
	@MyLog(module = "修改公告")
	@PutMapping("/update")
	public Result update(@RequestBody Notice notice){
		notice.setSum(0);
		notice.setConfirmed(null);
		noticeService.update(notice);
		return Result.ok("修改成功！");
	}
	
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Integer id){
		noticeService.deleteById(id);
		return Result.ok("删除成功！");
	}
	
	@DeleteMapping("/deleteAll/{ids}")
	public Result deleteAll(@PathVariable Integer[] ids){
		noticeService.deleteAll(ids);
		return Result.ok("删除成功！");
	}
	
	@PutMapping("/updateStatus/{id}/{status}")
	public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status){
		Notice notice=new Notice();
		notice.setId(id);
		notice.setStatus(status);
		notice.setSum(0);
		notice.setConfirmed(null);
		noticeService.update(notice);
		return Result.ok("更改成功！");
	}
	
	@PutMapping("/receive")
	public Result receive(@RequestBody Notice notice){
		User user=userService.getUser();
		if (!ObjectUtils.isEmpty(notice.getConfirmed())){
			noticeService.receive(notice.getId(), user);
			return Result.ok("已确认！");
		}
		notice.setConfirmed(user.getName());
		notice.setSum(1);
		System.out.println("通知信息："+notice);
		noticeService.update(notice);
		return Result.ok("已确认！");
	}
	
	@GetMapping("/selectNoticeCount")
	public Result selectNoticeCount(){
		List<TypeCountVO> list=noticeService.selectNoticeCount();
		System.out.println(list);
		return Result.ok(list);
	}
}

